﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace App.Contract
{
    public partial class frmPreliminaryEntry : Form
    {
        Entities db = new Entities();

        T_PreContract m_PreContract = null;

        private int m_pID = 0;

        private bool IsQuery = true;
        private bool bIsAdd = false;

        public int pID
        {
            set
            {
                var list = db.T_PreContract.Where(a => a.pID == value);
                foreach (var pre in list)
                {
                    m_PreContract = pre;
                    break;
                }
                if (m_PreContract == null) return;

                txtnewCtrNo.Text = m_PreContract.newCtrNo;
                if (m_PreContract.redo == "是") rbRedo1.Checked = true;
                else if (m_PreContract.redo == "否") rbRedo2.Checked = true;
                if (m_PreContract.isProducting == "是") chkisProducting.Checked = true;
                cbbProductNo.Text = m_PreContract.productNo;
                cbbsSize.Text = m_PreContract.sSize;
                txtrefCtrNo.Text = m_PreContract.refCtrNo;
                txtyard.Text = m_PreContract.yard;
                cbbBrand.Text = m_PreContract.brand;
                txtcolor.Text = m_PreContract.color;
                txtCustNo.Text = m_PreContract.Custom;
                txtFinalCustNo.Text = m_PreContract.finalCustNo;
                txtrefoPrice.Text = m_PreContract.refoPrice.ToString();
                txtMemo.Text=m_PreContract.memo;
                txtFactDateDesc.Text=m_PreContract.factDateDesc;
                nedtSoleprice4.Text=m_PreContract.refPrice4.ToString();
                nedtSoleprice3.Text=m_PreContract.refPrice3.ToString();
                nedtSoleprice2.Text=m_PreContract.refPrice2.ToString();
                nedtSoleprice1.Text=m_PreContract.refPrice1.ToString();
                //txtrefPrice4
                //txtrefPrice3.Text
                //txtrefPrice2.Text
                //txtrefPrice1.Text

                txtSoleyard4.Text=m_PreContract.yard4;
                txtSoleyard3.Text=m_PreContract.yard3;
                txtSoleyard2.Text=m_PreContract.yard2;
                txtSoleyard1.Text=m_PreContract.yard1;
                txtyGroup.Text=m_PreContract.yGroup;
                dtCustDate2.Text=m_PreContract.custDate2.ToString();
                txtnedtsCount.Text= m_PreContract.sCount.ToString();
                txtcustProdName.Text=m_PreContract.custProdName;
                txtyName.Text=m_PreContract.yName;
                dtCustDate1.Text=m_PreContract.custDate1.ToString();
                //txtHhb.Text  未知字段
                //txtRefHhb.Text
                cbbSoleFactory.Text=m_PreContract.SoleFactory;
                txtbmMaterial.Text=m_PreContract.bmMaterial;
                dtFactDate.Text=m_PreContract.factDate.ToString();
                cbbFinalCust.Text=m_PreContract.FinalCustom;
                cbbicurSystem.Text=m_PreContract.icurSystem;
                nedtinprice.Text=m_PreContract.inprice.ToString();
                //txtrefiPrice.Text  未知字段
                txtSoleNo.Text=m_PreContract.SoleNo;
                txtxtNo.Text=m_PreContract.xtNo;
                cbbFactory.Text=m_PreContract.factory;
                cbbCust.Text=m_PreContract.Custom;
                cbbCurSystem.Text=m_PreContract.curSystem;
                //picmNo.Text
                //picProductNo.Text
                txtrefYard1.Text=m_PreContract.refYard1;
                txtrefYard2.Text=m_PreContract.refYard2;
                txtrefYard3.Text=m_PreContract.refYard3;
                txtrefYard4.Text=m_PreContract.refYard4;
                if(m_PreContract.select1==true) txtrefPrice1.Checked=true;
                if(m_PreContract.select2==true) txtrefPrice2.Checked=true;
                if(m_PreContract.select3==true) txtrefPrice3.Checked=true;
                if(m_PreContract.select4==true) txtrefPrice4.Checked=true;
                txtrefsoleColor.Text=m_PreContract.refsoleColor;
                cbsole.Text = m_PreContract.sole;

            }
        }
        public frmPreliminaryEntry()
        {
            InitializeComponent();
            gvDataSource();
            this.panList.BringToFront();
        }

        private void gvDataSource()
        {
            var list = db.T_PreContract.Where(a => a.cdID == null).ToList();
            bs1.DataSource = list;
            bnav1.BindingSource = bs1;
            txtCount.Text = list.Sum(a => a.sCount).ToString();
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            gvID();
            if (this.IsQuery)
            {
                this.panDetail.BringToFront();
                this.btn1.Text = "概要";
                this.IsQuery = false;

            }
            else
            {
                this.panList.BringToFront();
                this.btn1.Text = "明细";
                this.IsQuery = true;
            }
        }

        private void gvID()
        {

            if (gvlist.RowCount > 0)
            {
                int index = gvlist.CurrentRow.Index;    //取得选中行的索引  
                this.pID = Convert.ToInt32(gvlist.Rows[index].Cells["colpID"].Value.ToString());
                this.m_pID = Convert.ToInt32(gvlist.Rows[index].Cells["colpID"].Value.ToString());
            }
            else
            {

            }

        }

        private void gvlist_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                this.pID = Convert.ToInt32(gvlist.Rows[e.RowIndex].Cells["colpID"].Value.ToString());
                this.panDetail.BringToFront();
                this.btn1.Text = "概要";
                this.IsQuery = false;
            }
            catch
            {
                Msg.ShowError("请选择表里的数据！");
            }
        }

        private void TxtClear()
        {
            txtnewCtrNo.Text = "UW"+DateTime.Now.ToString("yy")+"T";
            rbRedo1.Checked = true;
            rbRedo2.Checked = true;
            chkisProducting.Checked = false;
            cbbProductNo.Text = "";
            cbbsSize.Text = "";
            txtrefCtrNo.Text = "";
            txtyard.Text = "";
            cbbBrand.Text = "";
            txtcolor.Text = "";
            txtCustNo.Text = "";
            txtFinalCustNo.Text ="";
            txtrefoPrice.Text = "";
            txtMemo.Text = "";
            txtFactDateDesc.Text = "";
            nedtSoleprice4.Text = "";
            nedtSoleprice3.Text = "";
            nedtSoleprice2.Text = "";
            nedtSoleprice1.Text = "";
            txtrefPrice4.Text = "";
            txtrefPrice3.Text = "";
            txtrefPrice2.Text = "";
            txtrefPrice1.Text = "";
            txtSoleyard4.Text = "";
            txtSoleyard3.Text = ""; 
            txtSoleyard2.Text = "";
            txtSoleyard1.Text = "";
            txtyGroup.Text = "";
            dtCustDate2.Text = "";
            txtnedtsCount.Text = "";
            txtcustProdName.Text = "";
            rdbmType.Text = "";
            txtyName.Text = "";
            dtCustDate1.Text = "";
            txtHhb.Text = "";
            txtRefHhb.Text = "";
            cbbSoleFactory.Text = "";
            txtbmMaterial.Text = "";
            dtFactDate.Text = "";
            cbbFinalCust.Text = "";
            cbbicurSystem.Text = "";
            nedtinprice.Text = "";
            txtrefiPrice.Text = "";
            txtSoleNo.Text = "";
            txtxtNo.Text = "";
            cbbFactory.Text = "";
            cbbCust.Text = "";
            cbbCurSystem.Text = "";
            //picmNo.Text
            //picProductNo.Text
            txtrefYard1.Text = "";
            txtrefYard2.Text = "";
            txtrefYard3.Text = "";
            txtrefYard4.Text = "";
            txtrefPrice1.Checked = false;
            txtrefPrice2.Checked = false;
            txtrefPrice3.Checked = false;
            txtrefPrice4.Checked = false;
            txtrefYard1.Text = "";
            txtrefYard2.Text = "";
            txtrefYard3.Text = "";
            txtrefYard4.Text = "";
            txtrefsoleColor.Text = "";
            cbsole.Text = "";
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.panDetail.BringToFront();
            this.btn1.Text = "概要";
            this.IsQuery = false;

            this.bIsAdd = true;

            TxtClear();
        }

        private void SaveAdd()
        {
            m_PreContract = new T_PreContract();

            string dt=DateTime.Now.ToString("yyMMdd");
            m_PreContract.passelNo = dt + String.Format("{0:D2}", db.T_PreContract.Where(a => a.passelNo.Contains(dt)).Count() + 1);
            m_PreContract.newCtrNo=txtnewCtrNo.Text;
            if (rbRedo1.Checked == true) m_PreContract.redo = "是";
            else if (rbRedo2.Checked == true) m_PreContract.redo = "否";
            if (chkisProducting.Checked == true) m_PreContract.isProducting = "是";
            m_PreContract.productNo=cbbProductNo.Text;
            m_PreContract.sSize= cbbsSize.Text;
            m_PreContract.refCtrNo=txtrefCtrNo.Text;
            m_PreContract.yard=txtyard.Text;
            m_PreContract.brand=cbbBrand.Text;
            m_PreContract.color=txtcolor.Text;
            m_PreContract.Custom=txtCustNo.Text;
            m_PreContract.finalCustNo=txtFinalCustNo.Text;
            if(!string.IsNullOrEmpty(txtrefoPrice.Text))  m_PreContract.refoPrice=Helper.ToDecimal(txtrefoPrice.Text);
            m_PreContract.memo= txtMemo.Text;
            m_PreContract.factDateDesc= txtFactDateDesc.Text;
            if(!string.IsNullOrEmpty(nedtSoleprice4.Text)) m_PreContract.price4=Convert.ToInt32(nedtSoleprice4.Text) ;
            if(!string.IsNullOrEmpty(nedtSoleprice3.Text)) m_PreContract.price4=Convert.ToInt32(nedtSoleprice3.Text) ;
            if(!string.IsNullOrEmpty(nedtSoleprice2.Text)) m_PreContract.price4=Convert.ToInt32(nedtSoleprice2.Text) ;
            if(!string.IsNullOrEmpty(nedtSoleprice1.Text)) m_PreContract.price4=Convert.ToInt32(nedtSoleprice1.Text) ;
            if(txtrefPrice4.Checked==true) m_PreContract.select4=true;
            if(txtrefPrice3.Checked==true) m_PreContract.select3=true;
            if(txtrefPrice2.Checked==true) m_PreContract.select2=true;
            if(txtrefPrice1.Checked==true) m_PreContract.select1=true;
            m_PreContract.yard4= txtSoleyard4.Text;
             m_PreContract.yard4= txtSoleyard4.Text;
             m_PreContract.yard4= txtSoleyard4.Text;
             m_PreContract.yard4= txtSoleyard4.Text;
            m_PreContract.yGroup= txtyGroup.Text;
            if(!string.IsNullOrEmpty(dtCustDate2.Text)) m_PreContract.custDate2= Convert.ToDateTime( dtCustDate2.Text);
            if(!string.IsNullOrEmpty(txtnedtsCount.Text)) m_PreContract.sCount= Convert.ToInt32( txtnedtsCount.Text);
            m_PreContract.custProdName= txtcustProdName.Text;
            m_PreContract.yName= txtyName.Text;
            if(!string.IsNullOrEmpty(dtCustDate1.Text)) m_PreContract.custDate1= Convert.ToDateTime( dtCustDate1.Text);
            //txtHhb.Text 未知字段
            //txtRefHhb.Text
            m_PreContract.SoleFactory= cbbSoleFactory.Text;
            m_PreContract.bmMaterial= txtbmMaterial.Text;
            if(!string.IsNullOrEmpty(dtFactDate.Text)) m_PreContract.factDate= Convert.ToDateTime( dtFactDate.Text);
            m_PreContract.FinalCustom= cbbFinalCust.Text;
            m_PreContract.icurSystem= cbbicurSystem.Text;
            if(!string.IsNullOrEmpty( nedtinprice.Text)) m_PreContract.inprice= Convert.ToInt32( nedtinprice.Text);
            if(!string.IsNullOrEmpty(txtrefiPrice.Text)) m_PreContract.refiPrice= Convert.ToInt32( txtrefiPrice.Text);
            m_PreContract.SoleNo= txtSoleNo.Text;
            m_PreContract.xtNo= txtxtNo.Text;
            m_PreContract.factory= cbbFactory.Text;
            m_PreContract.Custom =cbbCust.Text;
            m_PreContract.curSystem= cbbCurSystem.Text;
            //picmNo.Text
            //picProductNo.Text
            m_PreContract.refYard1= txtrefYard1.Text;
            m_PreContract.refYard2= txtrefYard2.Text;
            m_PreContract.refYard3= txtrefYard3.Text;
            m_PreContract.refYard4= txtrefYard4.Text;
            if(txtrefPrice1.Checked==true) m_PreContract.select1=true;
            if(txtrefPrice2.Checked==true) m_PreContract.select2=true;
            if(txtrefPrice3.Checked==true) m_PreContract.select3=true;
            if(txtrefPrice4.Checked==true) m_PreContract.select4=true;
            m_PreContract.refsoleColor= txtrefsoleColor.Text;
            m_PreContract.sole = cbsole.Text;

            db.T_PreContract.Add(m_PreContract);
            int i = db.SaveChanges();
            Msg.ShowInfo(string.Format("已保存{0}条订单信息。", i));
            this.panList.BringToFront();
            this.btn1.Text = "明细";
            this.IsQuery = true;
        }

        private void SaveModify()
        {
            if (m_PreContract == null) return;

            m_PreContract.newCtrNo = txtnewCtrNo.Text;
            if (rbRedo1.Checked == true) m_PreContract.redo = "是";
            else if (rbRedo2.Checked == true) m_PreContract.redo = "否";
            if (chkisProducting.Checked == true) m_PreContract.isProducting = "是";
            m_PreContract.productNo = cbbProductNo.Text;
            m_PreContract.sSize = cbbsSize.Text;
            m_PreContract.refCtrNo = txtrefCtrNo.Text;
            m_PreContract.yard = txtyard.Text;
            m_PreContract.brand = cbbBrand.Text;
            m_PreContract.color = txtcolor.Text;
            m_PreContract.Custom = txtCustNo.Text;
            m_PreContract.finalCustNo = txtFinalCustNo.Text;
            if (!string.IsNullOrEmpty(txtrefoPrice.Text)) m_PreContract.refoPrice = Helper.ToDecimal(txtrefoPrice.Text);
            m_PreContract.memo = txtMemo.Text;
            m_PreContract.factDateDesc = txtFactDateDesc.Text;
            if (!string.IsNullOrEmpty(nedtSoleprice4.Text)) m_PreContract.price4 = Convert.ToInt32(nedtSoleprice4.Text);
            if (!string.IsNullOrEmpty(nedtSoleprice3.Text)) m_PreContract.price4 = Convert.ToInt32(nedtSoleprice3.Text);
            if (!string.IsNullOrEmpty(nedtSoleprice2.Text)) m_PreContract.price4 = Convert.ToInt32(nedtSoleprice2.Text);
            if (!string.IsNullOrEmpty(nedtSoleprice1.Text)) m_PreContract.price4 = Convert.ToInt32(nedtSoleprice1.Text);
            if (txtrefPrice4.Checked == true) m_PreContract.select4 = true;
            if (txtrefPrice3.Checked == true) m_PreContract.select3 = true;
            if (txtrefPrice2.Checked == true) m_PreContract.select2 = true;
            if (txtrefPrice1.Checked == true) m_PreContract.select1 = true;
            m_PreContract.yard4 = txtSoleyard4.Text;
            m_PreContract.yard4 = txtSoleyard4.Text;
            m_PreContract.yard4 = txtSoleyard4.Text;
            m_PreContract.yard4 = txtSoleyard4.Text;
            m_PreContract.yGroup = txtyGroup.Text;
            if (!string.IsNullOrEmpty(dtCustDate2.Text)) m_PreContract.custDate2 = Convert.ToDateTime(dtCustDate2.Text);
            if (!string.IsNullOrEmpty(txtnedtsCount.Text)) m_PreContract.sCount = Convert.ToInt32(txtnedtsCount.Text);
            m_PreContract.custProdName = txtcustProdName.Text;
            m_PreContract.yName = txtyName.Text;
            if (!string.IsNullOrEmpty(dtCustDate1.Text)) m_PreContract.custDate1 = Convert.ToDateTime(dtCustDate1.Text);
            //txtHhb.Text 未知字段
            //txtRefHhb.Text
            m_PreContract.SoleFactory = cbbSoleFactory.Text;
            m_PreContract.bmMaterial = txtbmMaterial.Text;
            if (!string.IsNullOrEmpty(dtFactDate.Text)) m_PreContract.factDate = Convert.ToDateTime(dtFactDate.Text);
            m_PreContract.FinalCustom = cbbFinalCust.Text;
            m_PreContract.icurSystem = cbbicurSystem.Text;
            if (!string.IsNullOrEmpty(nedtinprice.Text)) m_PreContract.inprice = Convert.ToInt32(nedtinprice.Text);
            if (!string.IsNullOrEmpty(txtrefiPrice.Text)) m_PreContract.refiPrice = Convert.ToInt32(txtrefiPrice.Text);
            m_PreContract.SoleNo = txtSoleNo.Text;
            m_PreContract.xtNo = txtxtNo.Text;
            m_PreContract.factory = cbbFactory.Text;
            m_PreContract.Custom = cbbCust.Text;
            m_PreContract.curSystem = cbbCurSystem.Text;
            //picmNo.Text
            //picProductNo.Text
            m_PreContract.refYard1 = txtrefYard1.Text;
            m_PreContract.refYard2 = txtrefYard2.Text;
            m_PreContract.refYard3 = txtrefYard3.Text;
            m_PreContract.refYard4 = txtrefYard4.Text;
            if (txtrefPrice1.Checked == true) m_PreContract.select1 = true;
            if (txtrefPrice2.Checked == true) m_PreContract.select2 = true;
            if (txtrefPrice3.Checked == true) m_PreContract.select3 = true;
            if (txtrefPrice4.Checked == true) m_PreContract.select4 = true;
            m_PreContract.refsoleColor = txtrefsoleColor.Text;
            m_PreContract.sole = cbsole.Text;

            int i = db.SaveChanges();
            Msg.ShowInfo(string.Format("已保存{0}条订单信息。", i));
            this.panList.BringToFront();
            this.btn1.Text = "明细";
            this.IsQuery = true;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btn1_Click(null, null);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Helper.TxtNotNull(txtyard) &&
                Helper.TxtNotNull(cbbProductNo) &&
                Helper.CBNotNull(cbbsSize) &&
                Helper.CBNotNull(cbbBrand) &&
                Helper.CBNotNull(cbbFinalCust) &&
                Helper.CBNotNull(cbbCust) &&
                Helper.CBNotNull(cbbFactory) &&
                !string.IsNullOrEmpty(dtFactDate.Text))
            {
                if (Msg.ShowWarning("您确信保存订单信息吗？") == System.Windows.Forms.DialogResult.Yes)
                {
                    if (m_PreContract == null)
                    {
                        SaveAdd();
                        gvDataSource();
                    }
                    else
                    {
                        SaveModify();
                        gvDataSource();
                    }
                }
            }
            else
            {
                Msg.ShowError("请填写必填项！");
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (Msg.ShowWarning("您确信删除信息吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                gvID();

                if (m_pID == 0)
                {
                    Msg.ShowError("请选择一条记录！");
                    return;
                }
                T_PreContract precon = db.T_PreContract.First(a => a.pID == m_pID);
                db.T_PreContract.Remove(precon);
                int i = db.SaveChanges();
                Msg.ShowInfo(string.Format("已删除{0}条订单信息。", i));
                gvDataSource();
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (Msg.ShowWarning("尚未保存，您确定要取消吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                this.panList.BringToFront();
                this.btn1.Text = "明细";
                this.IsQuery = true;
            }
        }

        private void frmPreliminaryEntry_Load(object sender, EventArgs e)
        {
            //帮面材料
            var list = db.T_MaterialDesc.Where(a => a.materialType == "帮面").Select(a => a.materialEN).ToList();
            txtbmMaterial.Data = list;
        }



    }
}
